{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "notebookRunGroups": {
     "groupValue": ""
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(R\"C:\\Users\\23864\\Desktop\\he.2022\\数据\\movie-country.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    59.000000\n",
       "mean      8.698305\n",
       "std       0.762141\n",
       "min       6.500000\n",
       "25%       8.450000\n",
       "50%       9.000000\n",
       "75%       9.200000\n",
       "max       9.700000\n",
       "Name: average, dtype: float64"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['average'].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Unnamed: 0        int64\n",
       "id              float64\n",
       "average         float64\n",
       "country          object\n",
       "genre            object\n",
       "language         object\n",
       "release_date     object\n",
       "title            object\n",
       "votes           float64\n",
       "产地               object\n",
       "dtype: object"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Unnamed: 0         int64\n",
       "id               float64\n",
       "average          float64\n",
       "country           object\n",
       "genre             object\n",
       "language          object\n",
       "release_date      object\n",
       "title             string\n",
       "votes              int32\n",
       "产地              category\n",
       "dtype: object"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#df.dropna()\n",
    "#df.astype({\"votes\":\"int\"})\n",
    "#df.dropna().astype({\"votes\":\"int\",\"title\":\"string\"}).dtypes\n",
    "df.dropna().astype({\"votes\":\"int\",\"title\":\"string\",\"产地\":\"category\"}).dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "#设置已有变量为类别变量    df2 = df.astype({\"产地\": \"category\"})\n",
    "#获取类别变量所有的类别取值 df2['产地'].cat.categories \n",
    "#增加某个类别变量的类别    df2['产地'].cat.add_categories(['俄罗斯'],inplace=True)\n",
    "#删除某个类别变量的类别    df2['产地'].cat.remove_categories(['俄罗斯'],inplace=True) \n",
    "#修改某个类别变量的类别    df2.产地.cat.rename_categories({'俄罗斯': '苏联','印度':'India',},inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['中国大陆', '中国香港', 'India', '意大利', '日本', '法国', '美国', '西班牙', '韩国', '黎巴嫩'], dtype='object')"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2['产地'].cat.categories "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\23864\\AppData\\Local\\Temp\\ipykernel_1244\\617321494.py:1: FutureWarning: The `inplace` parameter in pandas.Categorical.add_categories is deprecated and will be removed in a future version. Removing unused categories will always return a new Categorical object.\n",
      "  df2['产地'].cat.add_categories(['俄罗斯'],inplace=True)\n"
     ]
    }
   ],
   "source": [
    "df2['产地'].cat.add_categories(['俄罗斯'],inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\23864\\AppData\\Local\\Temp\\ipykernel_1244\\529300991.py:1: FutureWarning: The `inplace` parameter in pandas.Categorical.remove_categories is deprecated and will be removed in a future version. Removing unused categories will always return a new Categorical object.\n",
      "  df2['产地'].cat.remove_categories(['俄罗斯'],inplace=True)\n"
     ]
    }
   ],
   "source": [
    "df2['产地'].cat.remove_categories(['俄罗斯'],inplace=True) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\23864\\AppData\\Local\\Temp\\ipykernel_1244\\3140075114.py:1: FutureWarning: The `inplace` parameter in pandas.Categorical.rename_categories is deprecated and will be removed in a future version. Removing unused categories will always return a new Categorical object.\n",
      "  df2.产地.cat.rename_categories({'俄罗斯': '苏联','印度':'India',},inplace=True)\n"
     ]
    }
   ],
   "source": [
    "df2.产地.cat.rename_categories({'俄罗斯': '苏联','印度':'India',},inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pandas.api.types import CategoricalDtype\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['星级'] = pd.cut(df['average'],\n",
    "bins=[0, 2, 4, 6, 8, 10],\n",
    "labels=['一星', '二星', '三星', '四星', '五星'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      五星\n",
       "1      五星\n",
       "2      五星\n",
       "3      五星\n",
       "4      五星\n",
       "5      五星\n",
       "6      五星\n",
       "7      五星\n",
       "8      五星\n",
       "9      五星\n",
       "10     五星\n",
       "11     五星\n",
       "12     五星\n",
       "13     五星\n",
       "14     五星\n",
       "15     五星\n",
       "16     五星\n",
       "17     五星\n",
       "18     四星\n",
       "19     五星\n",
       "20     五星\n",
       "21     四星\n",
       "22     五星\n",
       "23     五星\n",
       "24     五星\n",
       "25     五星\n",
       "26     五星\n",
       "27     五星\n",
       "28     五星\n",
       "29     五星\n",
       "30     五星\n",
       "31     五星\n",
       "32     五星\n",
       "33     五星\n",
       "34     五星\n",
       "35     五星\n",
       "36     五星\n",
       "37     五星\n",
       "38     五星\n",
       "39     四星\n",
       "40     五星\n",
       "41     五星\n",
       "42     五星\n",
       "43     五星\n",
       "44     四星\n",
       "45     五星\n",
       "46     五星\n",
       "47     四星\n",
       "48     五星\n",
       "49     四星\n",
       "50     四星\n",
       "51     五星\n",
       "52     五星\n",
       "53     四星\n",
       "54     五星\n",
       "55     四星\n",
       "56     四星\n",
       "57     五星\n",
       "58     五星\n",
       "59    NaN\n",
       "Name: 星级, dtype: category\n",
       "Categories (5, object): ['一星' < '二星' < '三星' < '四星' < '五星']"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cat_type = CategoricalDtype(categories=['一星', '二星', '三星', '四星', '五星'], ordered=True)\n",
    "df['星级'].astype(cat_type)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_raw = pd.read_spss(R\"C:\\Users\\23864\\Desktop\\he.2022\\数据\\identity.sav\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [],
   "source": [
    "condition = '会以中国人自豪吗==\"会\" and 会隐瞒身份吗==\"一定会\"'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Int64Index([ 69, 183, 184, 294, 347, 374, 394, 412, 448, 467, 498, 561, 566,\n",
      "            595, 612, 615, 625, 643, 742, 745, 757, 769, 777, 813, 819, 824,\n",
      "            829, 836, 843, 848],\n",
      "           dtype='int64')\n"
     ]
    }
   ],
   "source": [
    "result = df_raw.query(condition).index\n",
    "print(result)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_raw.drop(index=result,inplace=True)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.10 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "138148c979a60859ae74ca41993c9becbe8ce800154b30dc52652dbd6e25207c"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
